package org.rednebula.framework.core.context;

import java.io.Serializable;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.rednebula.framework.core.BasicContext;
import org.rednebula.framework.core.utils.Struts2Utils;



public class RequestContext extends BasicContext implements Serializable {

	public RequestContext() {
		super();
		this.setName(REQUEST);
	}

	public void close() {

	}

	public HttpServletRequest getHttpRequest(){
		return Struts2Utils.getRequest();
	}
	public <T> T get(String key) {
		if(isSelf(key))return (T)this.getHttpRequest();
		return (T)getHttpRequest().getAttribute(key);
	}

	public <T> T get(Class<T> type) {
		return (T)getHttpRequest().getAttribute(type.getName());
	}

	public boolean has(String name) {
		if(name==null)return false;
		if(this.isSelf(name))return true;
		HttpServletRequest request=getHttpRequest();
		Enumeration<String> ns= request.getAttributeNames();
		while(ns.hasMoreElements()){
			if(name.equals(ns.nextElement()))return true;
		}
		return false;
	}

}
